package zhengyanze.com.aipoemmax.guide.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.fragment.filter.AuthorFragment;
import zhengyanze.com.aipoemmax.guide.fragment.filter.GuwenFragment;
import zhengyanze.com.aipoemmax.guide.fragment.filter.MingJuFragment;
import zhengyanze.com.aipoemmax.guide.utils.TypefaceUtil;

public class FilterActivity extends AppCompatActivity {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_zuozhe)
    TextView tvZuozhe;
    @BindView(R.id.tv_chaodai)
    TextView tvChaodai;
    @BindView(R.id.tv_mingju)
    TextView tvMingju;
    @BindView(R.id.fl)
    FrameLayout fl;
    private ArrayList<View> mView;

    MingJuFragment mingJuFragment = new MingJuFragment();
    GuwenFragment guwenFragment = new GuwenFragment();
    AuthorFragment authorFragment = new AuthorFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        tvTitle.setText("筛选");
        TypefaceUtil utils = new TypefaceUtil(FilterActivity.this, Constant.TTF.TTF_MAIN_TYPE);
        utils.setTypeface(tvTitle, false);
        mView = new ArrayList<>();
        mView.add(tvMingju);
        mView.add(tvChaodai);
        mView.add(tvZuozhe);
        switchTrue(0);
        replaceView(mingJuFragment);
    }

    @OnClick({R.id.tv_title, R.id.tv_zuozhe, R.id.tv_chaodai, R.id.tv_mingju})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_mingju:
                switchTrue(0);
                replaceView(mingJuFragment);
                break;
            case R.id.tv_chaodai:
                switchTrue(1);
                replaceView(guwenFragment);
                break;
            case R.id.tv_zuozhe:
                switchTrue(2);
                replaceView(authorFragment);
                break;
        }
    }

    public void switchTrue(int position) {
        for (int i = 0; i < mView.size(); i++) {
            View tv = mView.get(i);
            if (i == position) {
                tv.setBackgroundResource(R.mipmap.setting_send);
            } else {
                tv.setBackgroundColor(getResources().getColor(R.color.umeng_socialize_ucenter_bg));
            }
        }
    }


    List<Fragment> fragments = new ArrayList<>();
    /**
     * 碎片替换方法
     *
     * @param fragment
     */
    public void replaceView(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        //全部隐藏,再显示当前添加的碎片
        for (int i = 0; i < fragments.size(); i++) {
            transaction.hide(fragments.get(i));
        }
        if (fragment.isAdded()) {
            transaction.show(fragment);
        }else{
            fragments.add(fragment);
            transaction.add(R.id.fl, fragment);
        }
        transaction.commit();
    }
}
